package com.zoro.barn.commons.webcommons.tools;

import com.zoro.barn.commons.foundation.constants.web.RequestConstants;
import com.zoro.barn.commons.tools.encoding.Base64Util;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 请求上下文工具类
 *
 * @author zhaoxingwu
 */
public class WebContentUtil {

    /**
     * get {@link HttpServletRequest}
     *
     * @return request
     */
    public static HttpServletRequest getRequest() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (attributes instanceof ServletRequestAttributes) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attributes;
            return servletRequestAttributes.getRequest();
        }
        throw new RuntimeException("request can not obtain,please make sure this is a http request");
    }

    /**
     * get request uri
     *
     * @return request
     */
    public static String getUri() {
        return getRequest().getRequestURI();
    }

    /**
     * get query string
     *
     * @return query string
     */
    public static String getQueryString() {
        return getRequest().getQueryString();
    }

    /**
     * get input stream
     *
     * @return input stream
     */
    public static InputStream getInputStream() {
        try {
            return getRequest().getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取登陆人信息
     *
     * @return 登陆人信息
     */
    public static RequestUser requestUser() {
        HttpServletRequest request = getRequest();
        String account = Base64Util.urlDecoding(request.getHeader(RequestConstants.USER_ACCOUNT_HEADER));
        String userName = Base64Util.urlDecoding(request.getHeader(RequestConstants.USER_NAME_HEADER));
        String pid = request.getHeader(RequestConstants.USER_PID_HEADER);
        if (StringUtils.isAnyBlank(account, userName, pid)) {
            return null;
        } else {
            return new RequestUser(account, pid, userName);
        }
    }

    /**
     * 获取登陆人信息
     *
     * @return 登陆人信息
     */
    public static RequestUser requestUserInfo() {
        RequestUser user = requestUser();
        if (user == null) {
            throw new RuntimeException("can not obtain user,may be not login");
        } else {
            return user;
        }
    }

    /**
     * 获取当前请求的 response
     *
     * @return response
     */
    public static HttpServletResponse getResponse() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (attributes instanceof ServletRequestAttributes) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attributes;
            return servletRequestAttributes.getResponse();
        }
        throw new RuntimeException("response can not obtain,please make sure this is a http response");
    }

    public static OutputStream getResponseOutputStream() throws IOException {
        return getResponse().getOutputStream();
    }
}
